package com.cch.cooperation.api.zone.listener;

import com.cch.cooperation.api.auth.event.UserAddEvent;
import com.cch.cooperation.api.zone.service.ZoneServiceImpl;
import com.cch.cooperation.api.zone.service.ZoneUserServiceImpl;
import com.cch.cooperation.dao.zone.enums.ZoneEnum;
import com.cch.cooperation.dao.zone.enums.ZoneUserEnum;
import com.cch.cooperation.dao.zone.model.Zone;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 空间用户加入监听
 *
 * @author cch
 */
@Component
public class ZoneUserListener {

    @Resource
    private ZoneUserServiceImpl zoneUserService;
    @Resource
    private ZoneServiceImpl zoneService;

    @EventListener
    public void onUserAdd(UserAddEvent event) {
        if (Objects.isNull(event) || Objects.isNull(event.getUser())) {
            return;
        }
        List<Zone> zones = zoneService.queryByType(ZoneEnum.Type.OFFICIAL.value);
        for (Zone zone : zones) {
            zoneUserService.add(zone.getId(), event.getUser().getId(), ZoneUserEnum.RoleType.NORMAL.value);
        }
    }
}
